﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

public partial class Management_Product_ProductCategoryList : PageBase2
{
    private CategoryManager m_CategoryManager = new CategoryManager();
    private ProductManager m_ProductManager = new ProductManager();
    private RoleManager m_RoleManager = new RoleManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.SignInUser1 == null)
        {
            Session.Remove("SignInUser1");
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
            return;
        }
        if (!IsPostBack)
        {
            Cosmetic.DataEntity.User theUser = this.SignInUser1;
            if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
        }
        if (!IsPostBack)
        {
            this.BindSupCategory();
        }
    }



    private void BindSupCategory()
    {
        this.lvSupCategory.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L)).OrderBy(theCategory => theCategory.Index);
        this.lvSupCategory.DataBind();
    }

    private void BindSubCategory()
    {
        this.lvSubCategory.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(this.hfSupCategoryId.Value))).OrderBy(theCategory => theCategory.Index);
        this.lvSubCategory.DataBind();
    }

    private void BindThirdCategoryList()
    {
        if (!string.IsNullOrEmpty(hfSubCategoryId.Value))
        {
            this.lvThirdCategory.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(this.hfSubCategoryId.Value))).OrderBy(theCategory => theCategory.Index);
            this.lvThirdCategory.DataBind();
        }
        else
        {
            this.lvThirdCategory.DataSource = null;
            lvThirdCategory.DataBind();
        }
    }


    protected void lvSubCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("OnSubCategory"))
        {
            this.hfSubCategoryId.Value = e.CommandArgument.ToString();
            this.hfThirdCategoryId.Value = "";
            this.BindThirdCategoryList();
            return;
        }
        else if (e.CommandName.Equals("OnNew"))
        {
            this.lvSubCategory.InsertItemPosition = InsertItemPosition.LastItem;
        }
        else if (e.CommandName.Equals("OnInsert"))
        {
            Category theSupProductCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.hfSupCategoryId.Value));
            Category theNewProductCategory = new Category();
            theNewProductCategory.CategoryId = Guid.NewGuid();
            theNewProductCategory.CreateTime = DateTime.Now;
            theNewProductCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            theNewProductCategory.SupCategoryId = theSupProductCategory.CategoryId;
            this.m_CategoryManager.Insert(theNewProductCategory);

            this.lvSubCategory.InsertItemPosition = InsertItemPosition.None;
            this.BindSupCategory();
        }
        else if (e.CommandName.Equals("OnEdit"))
        {
            this.lvSubCategory.EditIndex = this.lvSubCategory.Items.IndexOf(e.Item as ListViewDataItem);

            //Category theCategory					= this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            //this.hfCategoryId.Value					= e.CommandArgument.ToString();

            //ddlSupCategory.SaveCategory = theCategory.SupCategory;
            ////CategoryCascadingDropDown1.SaveCategory = theCategory.SupCategory;
            //this.txtName.Text						= theCategory.Name;
            //this.mpeModify.Show();
        }
        else if (e.CommandName.Equals("OnDelete"))
        {
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            if (this.GetProductCount(theCategory.CategoryId) > 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("有产品正在使用该产品类别!，请先更改这些产品的类别!", this.Page);
                return;
            }
            else
            {
                this.m_CategoryManager.Delete(theCategory);
                this.BindSupCategory();
            }
            if (this.m_CategoryManager.GetSubCateogoriesBySupCategory(theCategory).Count > 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("该分类下包含有子分类，请先删除子分类!", this.Page);
                return;
            }
            else
            {
                this.m_CategoryManager.Delete(theCategory);
                this.BindSubCategory();
            }
        }
        else if (e.CommandName.Equals("OnCancel"))
        {
            this.lvSubCategory.EditIndex = -1;
            this.lvSubCategory.InsertItemPosition = InsertItemPosition.None;
        }
        else if (e.CommandName.Equals("OnUp"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最上面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvSubCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex - 1].Value.ToString()));
        }
        else if (e.CommandName.Equals("OnDown"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == this.lvSubCategory.Items.Count - 1)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最下面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvSubCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex + 1].Value.ToString()));
        }
        else if (e.CommandName.Equals("OnUpdate"))
        {
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            theCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            this.m_CategoryManager.Update();
            this.lvSubCategory.EditIndex = -1;
        }

        this.BindSubCategory();
    }

    protected void lvThirdCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("OnNew"))
        {
            this.lvThirdCategory.InsertItemPosition = InsertItemPosition.LastItem;
        }
        else if (e.CommandName.Equals("OnInsert"))
        {
            Category theSupProductCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.hfSubCategoryId.Value));
            Category theNewProductCategory = new Category();
            theNewProductCategory.CategoryId = Guid.NewGuid();
            theNewProductCategory.CreateTime = DateTime.Now;
            theNewProductCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            theNewProductCategory.SupCategoryId = theSupProductCategory.CategoryId;
            this.m_CategoryManager.Insert(theNewProductCategory);

            this.lvThirdCategory.InsertItemPosition = InsertItemPosition.None;
            this.BindSupCategory();
        }
        else if (e.CommandName.Equals("OnEdit"))
        {
            this.lvThirdCategory.EditIndex = this.lvThirdCategory.Items.IndexOf(e.Item as ListViewDataItem);

            //Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));

            //this.hfThirdCategoryId.Value = e.CommandArgument.ToString();

            ////ddlSupCategory.SaveCategory = this.m_CategoryManager.GetCategoryByCategoryID(new Guid(hfSubCategoryId.Value)); //theCategory.SupCategory;

            ////CategoryCascadingDropDown1.SaveCategory = ddlSupCategory.SaveCategory;
            //this.txtName.Text = theCategory.Name;
            //this.mpeModify.Show();
        }
        else if (e.CommandName.Equals("OnDelete"))
        {
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            if (this.GetProductCount(theCategory.CategoryId) > 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("有产品正在使用该产品类别!，请先更改这些产品的类别!", this.Page);
                return;
            }
            else
            {
                this.m_CategoryManager.Delete(theCategory);
                this.BindSupCategory();
            }
        }
        else if (e.CommandName.Equals("OnCancel"))
        {
            this.lvThirdCategory.EditIndex = -1;
            this.lvThirdCategory.InsertItemPosition = InsertItemPosition.None;
        }
        else if (e.CommandName.Equals("OnUp"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最上面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvThirdCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex - 1].Value.ToString()));
        }
        else if (e.CommandName.Equals("OnDown"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == this.lvThirdCategory.Items.Count - 1)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最下面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvThirdCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex + 1].Value.ToString()));
        }
        else if (e.CommandName.Equals("OnUpdate"))
        {
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            theCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            this.m_CategoryManager.Update();
            this.lvThirdCategory.EditIndex = -1;
        }
        this.BindThirdCategoryList();
    }


    //protected string SendPictureFile()
    //{
    //    if (fuCategoryPicture.HasFile)
    //    {
    //        string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["CategoryPath"]);
    //        string theFileName = string.Empty;
    //        if (String.IsNullOrEmpty(this.hfCategoryId.Value))
    //        {
    //            theFileName = Guid.NewGuid().ToString() + fuCategoryPicture.FileName.Substring(this.fuCategoryPicture.FileName.LastIndexOf('.'));
    //        }
    //        else
    //        {
    //            theFileName = this.hfCategoryId.Value + fuCategoryPicture.FileName.Substring(this.fuCategoryPicture.FileName.LastIndexOf('.'));
    //        }
    //        if (File.Exists(theFilePath + theFileName))
    //        {
    //            File.Delete(theFilePath + theFileName);
    //        }

    //        this.fuCategoryPicture.SaveAs(theFilePath + theFileName);
    //        return ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
    //        //this.Image1.ImageUrl = ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
    //        //this.Image1.Visible = true;
    //        //this.txtShowPicturePath.Text = this.Image1.ImageUrl;
    //    }
    //    else
    //    {
    //        return null;
    //    }
    //}

    //protected string SendPictureFile2()
    //{
    //    if (fuCategoryPicture2.HasFile)
    //    {
    //        string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["CategoryPath"]);
    //        string theFileName = string.Empty;
    //        if (String.IsNullOrEmpty(this.hfCategoryId2.Value))
    //        {
    //            theFileName = Guid.NewGuid().ToString() + fuCategoryPicture2.FileName.Substring(this.fuCategoryPicture2.FileName.LastIndexOf('.'));
    //        }
    //        else
    //        {
    //            theFileName = this.hfCategoryId2.Value + fuCategoryPicture2.FileName.Substring(this.fuCategoryPicture2.FileName.LastIndexOf('.'));
    //        }
    //        if (File.Exists(theFilePath + theFileName))
    //        {
    //            File.Delete(theFilePath + theFileName);
    //        }

    //        this.fuCategoryPicture2.SaveAs(theFilePath + theFileName);
    //        return ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
    //        //this.Image1.ImageUrl = ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
    //        //this.Image1.Visible = true;
    //        //this.txtShowPicturePath.Text = this.Image1.ImageUrl;
    //    }
    //    else
    //    {
    //        return null;
    //    }
    //}

    protected string SendPictureFileGrid(FileUpload gvFileUpload)
    {
        if (gvFileUpload.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["CategoryPath"]);
            string theFileName = string.Empty;
            if (String.IsNullOrEmpty(this.hfCategoryId2.Value))
            {
                theFileName = Guid.NewGuid().ToString() + gvFileUpload.FileName.Substring(gvFileUpload.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName = this.hfCategoryId2.Value + gvFileUpload.FileName.Substring(gvFileUpload.FileName.LastIndexOf('.'));
            }
            if (File.Exists(theFilePath + theFileName))
            {
                File.Delete(theFilePath + theFileName);
            }

            gvFileUpload.SaveAs(theFilePath + theFileName);
            return ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
            //this.Image1.ImageUrl = ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
            //this.Image1.Visible = true;
            //this.txtShowPicturePath.Text = this.Image1.ImageUrl;
        }
        else
        {
            return null;
        }
    }

    protected void lvSupCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //if (e.CommandName.Equals("OnEditPic"))
        //{
        //    //Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
        //    this.hfCategoryId2.Value = e.CommandArgument.ToString();

        //    //ddlSupCategory.SaveCategory = theCategory.SupCategory;
        //    //CategoryCascadingDropDown1.SaveCategory = theCategory.SupCategory;
        //    //this.txtName.Text = theCategory.Name;
        //    this.mpeModify2.Show();
        //}
        if (e.CommandName.Equals("OnSubCategory"))
        {
            this.hfSupCategoryId.Value = e.CommandArgument.ToString();
            this.hfSubCategoryId.Value = "";
            this.BindSubCategory();
            //this.BindThirdCategoryList();
            return;
        }
        else if (e.CommandName.Equals("OnNew"))
        {
            this.lvSupCategory.InsertItemPosition = InsertItemPosition.LastItem;
        }
        else if (e.CommandName.Equals("OnInsert"))
        {
            //if (m_CategoryManager.IsExistTheName((e.Item.FindControl("txtName") as TextBox).Text.Trim()))
            //{
            //    Cosmetic.WebGeneric.WebMessageBox("该类下包含有此类！", this.Page);
            //    return;
            //}
            Category theTopProductCategory = this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L);
            Category theNewProductCategory = new Category();
            theNewProductCategory.CategoryId = Guid.NewGuid();
            theNewProductCategory.CreateTime = DateTime.Now;
            theNewProductCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            string picurl = SendPictureFileGrid(e.Item.FindControl("categoryPic") as FileUpload);
            if (picurl != null)
            {
                theNewProductCategory.PictureUrl = picurl;
            }

            theNewProductCategory.SupCategoryId = theTopProductCategory.CategoryId;
            this.m_CategoryManager.Insert(theNewProductCategory);

            this.lvSupCategory.InsertItemPosition = InsertItemPosition.None;
        }
        else if (e.CommandName.Equals("OnEdit"))
        {
            this.hfCategoryId2.Value = e.CommandArgument.ToString();
            this.lvSupCategory.EditIndex = this.lvSupCategory.Items.IndexOf(e.Item as ListViewDataItem);
        }
        else if (e.CommandName.Equals("OnUpdate"))
        {
            FileUpload categoryPic = e.Item.FindControl("categoryPic") as FileUpload;
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            theCategory.Name = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
            string picurl = SendPictureFileGrid(categoryPic);
            if (picurl != null)
            {
                theCategory.PictureUrl = picurl;
            }
            this.m_CategoryManager.Update();
            this.lvSupCategory.EditIndex = -1;
        }
        else if (e.CommandName.Equals("OnDelete"))
        {
            Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));
            if (this.m_CategoryManager.GetSubCateogoriesBySupCategory(theCategory).Count > 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("该分类下包含有子分类，请先删除子分类!", this.Page);
                return;
            }
            else
            {
                this.m_CategoryManager.Delete(theCategory);
            }
        }
        else if (e.CommandName.Equals("OnCancel"))
        {
            this.lvSupCategory.EditIndex = -1;
            this.lvSupCategory.InsertItemPosition = InsertItemPosition.None;
        }
        else if (e.CommandName.Equals("OnUp"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == 0)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最上面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvSupCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex - 1].Value.ToString()));
        }
        else if (e.CommandName.Equals("OnDown"))
        {
            if ((e.Item as ListViewDataItem).DataItemIndex == this.lvSupCategory.Items.Count - 1)
            {
                Cosmetic.WebGeneric.WebMessageBox("已经是最下面!", this.Page);
                return;
            }
            this.Exchange(new Guid(e.CommandArgument.ToString()), new Guid(this.lvSupCategory.DataKeys[(e.Item as ListViewDataItem).DataItemIndex + 1].Value.ToString()));
        }
        this.BindSupCategory();
    }


    private void Exchange(Guid aCategoryIdA, Guid aCategoryIdB)
    {
        Category theCategoryA = this.m_CategoryManager.GetCategoryById(aCategoryIdA);
        Category theCategoryB = this.m_CategoryManager.GetCategoryById(aCategoryIdB);

        int theIndex = theCategoryA.Index;
        theCategoryA.Index = theCategoryB.Index;
        theCategoryB.Index = theIndex;

        this.m_CategoryManager.Update();
    }

    //protected void btnSubmit_Click(object sender, EventArgs e)
    //{
    //    if (String.IsNullOrEmpty(this.txtName.Text))
    //    {
    //        this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('产品类别名称不能为空!');", true);
    //        this.mpeModify.Show();
    //        return;
    //    }

    //    Category theCategory = m_CategoryManager.GetCategoryById(new Guid(this.hfCategoryId.Value));
    //    // theCategory.SupCategory = m_CategoryManager.GetFrontCategoryById(theCategory.);//ddlSupCategory.SelectedCategory;
    //    //theCategory.SupCategory = CategoryCascadingDropDown1.SaveCategory;
    //    //if (this.m_CategoryManager.CheckContains(theCategory.SupCategory, this.txtName.Text.Trim()))
    //    //{
    //    //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('已经有同名产品类别!');", true);
    //    //    this.mpeModify.Show();
    //    //    return;
    //    //}
    //    theCategory.PictureUrl = SendPictureFile();
    //    theCategory.Name = this.txtName.Text.Trim();

    //    this.m_CategoryManager.Update();

    //    this.BindSupCategory();
    //    this.BindSubCategory();
    //    //this.BindThirdCategoryList();
    //}


    public int GetProductCount(Guid aCategoryId)
    {
        return this.m_ProductManager.GetProductByCategory(aCategoryId).Count;
    }

    //protected void btnSubmit2_Click(object sender, EventArgs e)
    //{
    //    Category theCategory = m_CategoryManager.GetCategoryById(new Guid(this.hfCategoryId2.Value));

    //    theCategory.PictureUrl = SendPictureFile2();
    //    //theCategory.Name = this.txtName.Text.Trim();

    //    this.m_CategoryManager.Update();

    //    this.BindSupCategory();
    //    //this.BindSubCategory();
    //}

    //protected void lvThirdCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
    //{
    //    if (e.CommandName.Equals("OnSubCategory"))
    //    {
    //        this.hfSupCategoryId.Value = e.CommandArgument.ToString();
    //        this.hfSubCategoryId.Value = "";
    //        this.BindSubCategory();

    //        return;
    //    }
    //}


    //protected void btnDeleteCategory_Click(object sender, EventArgs e)
    //{
    //    if (this.ddlSupCategory.SelectedCategory.SubCategories.Count > 0)
    //    {
    //        Cosmetic.WebGeneric.WebMessageBox("请先删除该产品类别的子类别!", this.Page);
    //        return;
    //    }
    //    this.m_CategoryManager.Delete(this.ddlSupCategory.SelectedCategory);
    //    this.ddlSupCategory.Refresh();
    //}

}
